package com.costeaalex.datedrivenfilecopy.controller;

/**
 * @author Alex Costea
 * 
 * 
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.costeaalex.datedrivenfilecopy.factories.CopierFactory;
import com.costeaalex.datedrivenfilecopy.factories.EraserFactory;
import com.costeaalex.datedrivenfilecopy.gui.Gui;
import com.costeaalex.datedrivenfilecopy.model.CopyModel;
import com.costeaalex.datedrivenfilecopy.model.EraserModel;
import com.costeaalex.datedrivenfilecopy.model.erasers.EraseFile;
import com.costeaalex.datedrivenfilecopy.model.interfaces.ICopier;
import com.costeaalex.datedrivenfilecopy.model.interfaces.IEraser;

public class Controller implements ActionListener, Observer{
	
	private Gui g;
	private CopyModel cM;
	private Thread sTCopy;
	private Thread sTErase;
	
	public Controller() {
		
	}
	
	public void init() {
		g=new Gui();
		g.buildGui();
		g.registerController(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if( ((JButton)e.getSource()).getText() == "Go") {
	    	if(g.isSelectedErase()) {
	    		EraserFactory eF=new EraserFactory();
	    		IEraser eraser;
	    		if(g.isSelectedFTP()) {
	    			eF.setFTPConnectionData(g.getConnectionData());
	    			eraser=eF.getEraser(2);
	    		}
	    		else
	    			eraser=eF.getEraser(1);
	    		EraserModel eM=new EraserModel(eraser,new EraseFile(g.getDestination(), true));
	    		sTErase=new Thread(eM);
	    		eM.addObserver(this);
	    		eM.addObserver(g);
	    		g.setStatus("Erasing...");
	    		sTErase.start();
	    	}
	    else
	    	runCopy();	
		}
		
		if(((JButton)e.getSource()).getText() == "Save") {
			g.hideFTPPopup();
		}
		
		if(((JButton)e.getSource()).getText() == "Set current time") {
			g.setCurrentTime();
		}
		
		if(((JButton)e.getSource()).getText() == "Select source") {
			g.showSourceSelectionPopup();
		}
		
		if(((JButton)e.getSource()).getText() == "Select destination") {
			g.showDestinationSelectionPopup();
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof String && arg=="Done erasing") {//when eraser is done
			o.deleteObserver(this);
    		o.deleteObserver(g);
			runCopy();
		}
	}
	
	private void runCopy() {
		try {
			CopierFactory cF=new CopierFactory();
			ICopier iC;
			if(g.isSelectedFTP()) {
				cF.setFTPConnectionData(g.getConnectionData());
				iC=cF.getCopier(2);
			}
			else
				iC=cF.getCopier(1);
			
			cM=new CopyModel(g.getSource(), g.getDestination(), g.getDate(), iC);
		}
		catch(Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "You've not selected source/destination directories!");
			return;
		}
		g.setGoEnabled(false);
		sTCopy=new Thread(cM);
		cM.addObserver(g);
		g.setStatus("Copying...");
		sTCopy.start();
	}
}
